import axios from 'axios';
import api from './request'

//时间工具
export const gettime = (data: any = {}) => {
    let dateStr = data.data;
    // 处理不同格式的日期字符串
    if (typeof dateStr === 'string') {
        // 去除可能的时区信息
        dateStr = dateStr.replace(/\s*GMT[+-]\d{4}\s*\(.+\)/, '');

        // 处理 '2024-10-1516:00:33' 这种格式
        const match = dateStr.match(/^(\d{4})-(\d{2})(\d{2})(\d{2}):(\d{2}):(\d{2})$/);
        if (match) {
            dateStr = `${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}:${match[6]}`;
        }
    }

    const date = new Date(dateStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    let obj = { updatedata: "", updatetime: "" };

    if (isNaN(year)) {
        // console.error('Invalid date format');
        // 如果日期无效，尝试从原始数据中提取日期和时间
        if (typeof dateStr === 'string') {
            const datePart = dateStr.slice(0, 10);
            const timePart = dateStr.slice(-8);
            obj.updatedata = datePart;
            obj.updatetime = timePart;
        }
    } else {
        obj.updatedata = `${year}-${month}-${day}`;
        obj.updatetime = `${hours}:${minutes}:${seconds}`;
    }

    console.log(obj);

    return obj;
}
//时间工具
export const gettime02 = (data: any = {}) => {
    
    // 处理不同格式的日期字符串
    if (typeof data === 'string') {
        // 去除可能的时区信息
        data = data.replace(/\s*GMT[+-]\d{4}\s*\(.+\)/, '');

        // 处理 '2024-10-1516:00:33' 这种格式
        const match = data.match(/^(\d{4})-(\d{2})(\d{2})(\d{2}):(\d{2}):(\d{2})$/);
        if (match) {
            data = `${match[1]}-${match[2]}-${match[3]}T${match[4]}:${match[5]}:${match[6]}`;
        }
    }

    const date = new Date(data);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    let obj = { updatedata: "", updatetime: "" };

    if (isNaN(year)) {
        // console.error('Invalid date format');
        // 如果日期无效，尝试从原始数据中提取日期和时间
        if (typeof data === 'string') {
            const datePart = data.slice(0, 10);
            const timePart = data.slice(-8);
            obj.updatedata = datePart;
            obj.updatetime = timePart;
        }
    } else {
        obj.updatedata = `${year}-${month}-${day}`;
        obj.updatetime = `${hours}:${minutes}:${seconds}`;
    }

    // console.log(obj);

    return obj;
}

//管理员登录
export const signin = async (data = {}) => {
    return axios.post(
        `${api.url}/api/user/signin`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
    )
        .then(response => response.data)
}

//验证码
export const test = async (data = {}) => {
    return axios.get(`${api.url}/api/verify/test`,
        {
            params: data
        }
    )
        .then(response => response.data)
}

//上传图片
export const imggetup = async (data = {}) => {
    return axios.post(`${api.url}/api/photo/upload`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }

    )
        .then(response => response.data)
}

//删除图片
export const imgdelete = async (data = {}) => {
    return axios.post(`${api.url}/api/photo/delete`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }

    )
        .then(response => response.data)
}

//库存列表
export const kuList = async (data = {}) => {
    return axios.get(`${api.url}/api/exam/category_list01`,
        {
            params: data
        }
    )
        .then(response => response.data)
}

//库存添加
export const kuAdd = async (data = {}) => {
    return axios.post(`${api.url}/api/exam/topic_add`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }

    )
        .then(response => response.data)
}
//仓库查询
export const kuListall = async (data = {}) => {
    return axios.get(`${api.url}/api/exam/category_list01`,
        {
            params: data
        }
    )
        .then(response => response.data)
}

//库存渲染
export const kuListall01 = async (data = {}) => {
    return axios.get(`${api.url}/api/exam/topic_list`,
        {
            params: data
        }
    )
        .then(response => response.data)
}
//更改数据
export const alter = async (data = {}) => {
    return axios.post(`${api.url}/api/exam/category_modify`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }

    )
        .then(response => response.data)
}

//新增仓库列表
//fid,kid,ku_name,site,functionary,phone,type,img,updatedata,updatetime
export const ku_add = async (data={})=>{
    return axios.post(`${api.url}/api/exam/exam_add`,
    data,
    {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }

)
    .then(response => response.data)
}

//查询库
export const ku_search = async (data={})=>{
    return axios.get(`${api.url}/api/user/search`,
    {
        params:data
    }
)
    .then(response => response.data)
}
//修改库
//fid,kid,ku_limit,ku_name,site,functionary,phone,type,img,updatedata,updatetime 
export const ku_alter = async (data={})=>{
    return axios.post(`${api.url}/api/exam/category_modify_ku`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}

//删除库存内容
//http://127.0.0.1:4000/api/exam/category_remove
export const ku_delete = async (data={})=>{
    return axios.get(`${api.url}/api/exam/category_remove`,{
        params:data
    })
    .then(response => response.data)
}

//入库
export const rukuApi = async (data={})=>{
    return axios.post(`${api.url}/api/exam/ruku`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}
//修改库存
export const amend_amount = async (data={})=>{
    return axios.post(`${api.url}/api/exam/category_modify01`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}

//修改库存
export const search01 = async (data={})=>{
  
    return axios.get(`${api.url}/api/user/search_ku`,{
        params:data,
    })
    .then(response => response.data)
}
//添加时间
export const category_add01 = async (data={})=>{
    return axios.post(`${api.url}/api/exam/category_add01`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}
//获取用户信息
export const get_user_info = async (data={})=>{
    return axios.post(`${api.url}/api/user/info01`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}
//获取操作接口
export const getcaozuo = async (data={})=>{
    return axios.get(`${api.url}/api/exam/category_list03`,{
        params:data
    })
    .then(response => response.data)
}
//获取全部用户
export const getuserou =async (data={})=>{
    return axios.get(`${api.url}/api/user/list01`,{
        params:data
    })
    .then(response => response.data)
}
//查询用户
export const searchUser =async (data={})=>{
    return axios.get(`${api.url}/api/user/search_user`,{
        params:data
    })
    .then(response => response.data)
}
//修改用户
export const alterUser =async (data={})=>{
    return axios.post(`${api.url}/api/user/edit_ku`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}
//添加用户
export const addUser =async (data={})=>{
    return axios.post(`${api.url}/api/user/user_add`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}

//删除用户
export const deleteUser =async (data={})=>{
    return axios.post(`${api.url}/api/user/remove_user`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
)
    .then(response => response.data)
}